package com.ybxx.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.Permission;
import com.ybxx.system.service.PermissionService;

public class PermissionAction extends BaseAction {
	@Resource
	private PermissionService permissionService;
	private String loginid;
	private List<Permission> permissions;
	private JSONObject resultObj;
	private String msg;
	private Permission permission;
	private String pid;
	public String queryUser(){
		permissions=permissionService.queryUserPermission(loginid);
		JSONArray arraySon=wrapUserPermissionJson(permissions);
		resultObj=new JSONObject();
		resultObj.put("menus", arraySon);
		return "queryUser";
	}
	private JSONArray wrapUserPermissionJson(List<Permission> permissions){
		JSONArray arrayOne =new JSONArray();
		for(Permission permission:permissions){
			JSONObject jsobjOne=new JSONObject();
			jsobjOne.put("id", permission.getPid());
			jsobjOne.put("name",permission.getPname());
			jsobjOne.put("pId",permission.getFid()==null?"0":permission.getFid());
			jsobjOne.put("checked",permission.getChecked());
			List<Permission> childmenus = new ArrayList(permission.getChilds());
			//若有子集
			if(childmenus.size()>0){
				for(Permission child:childmenus){
					JSONObject childJSON=new JSONObject();
					childJSON.put("id", child.getPid());
					childJSON.put("name",child.getPname());
					childJSON.put("pId",child.getFid()==null?"0":child.getFid());
					childJSON.put("checked",child.getChecked());
					arrayOne.add(childJSON);
				}
			}
			arrayOne.add(jsobjOne);
		}
		return arrayOne;
	}
	public String remove(){
		msg="succ";
		try{
			permissionService.remove(pid);
		}catch(Exception ex){
			msg="权限删除失败";
		}
		return "succ";
	}
	
	public String save(){
		msg="succ";
		try{
			if(permission!=null && permission.getPid()!=null && !permission.getPid().equals("")){
				if(permission.getPid().startsWith("T")){
					permission.setPid(UUIDGenerator.getUUID());
				}		
			}else{
				permission.setPid(UUIDGenerator.getUUID());
			}
			if(permission!=null && (permission.getParent()==null || ("").equals(permission.getParent().getPid()))){
				permission.setParent(null);
			}
			permissionService.save(permission);
		}catch(Exception e){
			msg="权限维护失败";
		}finally{
			return "succ";
		}
	}
	
	public String loadParent(){
		permissions=permissionService.loadParent();
		return "loadParent";
	}
	
	public String load(){
		List<Permission> permissions=permissionService.load(loginid);
		super.getSession().setAttribute(SystemConstants.SESSION_CURRENT_Permission, permissions);
		if(permissions!=null && permissions.size()>0){
			Permission permission=permissions.get(0);
			if(permission.getSortChilds().size()>0){
				Permission child=permission.getSortChilds().get(0);
				super.getSession().setAttribute(SystemConstants.SESSION_CURRENT_MAIN, child.getPurl());
				msg=child.getPurl();
			}
		}
		return "succ";
	}
	
	public String loadAll(){
		permissions=permissionService.getAll();
		JSONArray arraySon=wrapPermissionJson(permissions);
		resultObj=new JSONObject();
		resultObj.put("menus", arraySon);
		return "loadAll";
	}
	private JSONArray wrapPermissionJson(List<Permission> permissions){
		JSONArray arrayOne =new JSONArray();
		for(Permission permission:permissions){
			JSONObject jsobjOne=new JSONObject();
			jsobjOne.put("id", permission.getPid());
			jsobjOne.put("name",permission.getPname());
			jsobjOne.put("pId",permission.getParent()==null?"0":permission.getParent().getPid());
			jsobjOne.put("purl",permission.getPurl());
			jsobjOne.put("seq",permission.getSeq());
			jsobjOne.put("style",permission.getStyle());
			List<Permission> childmenus = new ArrayList(permission.getChilds());
			//若有子集
			if(childmenus.size()>0){
				for(Permission child:childmenus){
					JSONObject childJSON=new JSONObject();
					childJSON.put("id", child.getPid());
					childJSON.put("name",child.getPname());
					childJSON.put("pId",child.getParent().getPid());
					childJSON.put("purl",child.getPurl());
					childJSON.put("seq",child.getSeq());
					childJSON.put("style",child.getStyle());
					arrayOne.add(childJSON);
				}
			}
			arrayOne.add(jsobjOne);
		}
		return arrayOne;
	}

	public String getLoginid() {
		return loginid;
	}

	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public JSONObject getResultObj() {
		return resultObj;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}
	
}
